var store = {

}
store.addProduct = function( data ){

    let cartList = wx.getStorageSync('cart')

    if(cartList){
        let arr = JSON.parse( cartList );
        arr.push(data);


        wx.setStorageSync("cart" ,  JSON.stringify(arr));

    }else{
        var arr = [];
        wx.setStorageSync("cart" ,  JSON.stringify(arr));
    }

    return true;

}


store.deleteProduct = function(  product_id ){

    let cartList = wx.getStorageSync('cart');

    if(cartList){

        var arr = JSON.parse(cartList);
        var index = -1;
        for(var i=0;i<arr.length ; i++){

            if(arr[i].id == product_id){
                index = i;
            }
        }

        if(index>-1){
            arr.splice(index , 1);
        }


        wx.setStorageSync("cart" , JSON.stringify(arr))
    }
}


store.getProductList = function(){
    try{

        let cartList = wx.getStorageSync('cart')
        if(cartList){
            var arr = JSON.parse(cartList);
            return arr;
        }

    }catch(e){

        return [];
    }
}

/**
 * 清空购物车
 */
store.clear = function(){
    var arr = [];

    wx.setStorageSync("cart" ,  JSON.stringify(arr));

}

store.set_cart_product_sums = function( nums ){
    wx.setStorageSync('cart_product_nums', nums);
}

store.get_cart_product_sums = function( ){
    var nums = wx.getStorageSync("cart_product_nums" , false);
    return nums;
}


export default store;